package com.example.myapplication.fragments;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.MainActivity;
import com.example.myapplication.R;
import com.example.myapplication.adapter.ClassMateListAdapter;
import com.example.myapplication.bean.ClassMateListBean;
import com.example.myapplication.utli.UtilsEnum;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment1 extends Fragment {
    private static final String TAG = "Fragment1";
    List<ClassMateListBean.DataDTO> mydata;
    ListView mLvClassMate;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Fragment1() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment1.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment1 newInstance(String param1, String param2) {
        Fragment1 fragment = new Fragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MainActivity activity = (MainActivity) getActivity();

        // 创建请求队列
        RequestQueue queue = Volley.newRequestQueue(getContext());
        // 创建请求
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("major", "移动应用开发");

        } catch (Exception e) {

        }

        JsonObjectRequest request = new JsonObjectRequest(
                Request.Method.POST,
                UtilsEnum.URL.url + "classmate/list?major=移动应用开发&department=信息学院",
                jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.e(TAG, "onResponse: " + jsonObject);

                        Gson gson = new Gson();
                        ClassMateListBean classMateListBean
                                = gson.fromJson(jsonObject.toString(), ClassMateListBean.class);
                        List<ClassMateListBean.DataDTO> data = classMateListBean.getData();
                        mydata = data;
                        // 更新界面
                        ClassMateListAdapter adapter = new ClassMateListAdapter(data, getContext());
                        mLvClassMate.setAdapter(adapter);

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e(TAG, "onErrorResponse: " + volleyError);
                    }
                }
        );
        // 将请求加入队列
        queue.add(request);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //Bundle arguments = getArguments();
        //String title = arguments.getString("title");
        //TextView mTvTitle = view.findViewById(R.id.tv_title);
        // mTvTitle.setText(title);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // Inflate the layout for this fragmentfragmentManager = getSupportFragmentManager();
        //                        transaction = fragmentManager.beginTransaction();
        //                        transaction.replace(R.id.fl_container,new Fragment1());
        //                        transaction.commit();
        View view = inflater.inflate(R.layout.fragment_1, container, false);
        mLvClassMate = view.findViewById(R.id.lv_classmate);
        mLvClassMate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                StudentFragment fragment = new StudentFragment();
                Bundle args = new Bundle();
                Log.e(TAG, "onItemClick: "+mydata.get(position).getId() );
                args.putInt("id", mydata.get(position).getId());
                fragment.setArguments(args);
                transaction.replace(R.id.fl_container, fragment);
                transaction.commit();
            }
        });
        return view;
    }
}